package com.guguskins.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guguskins.back.entity.GuFeedback;
import com.guguskins.back.entity.GuFeedbackType;
import com.guguskins.back.entity.GuUser;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.mapper.GuFeedbackMapper;
import com.guguskins.back.service.GuFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.service.GuFeedbackTypeService;
import com.guguskins.back.service.GuUserService;
import com.guguskins.back.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 问题反馈表 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-10
 */
@Service
public class GuFeedbackServiceImpl extends ServiceImpl<GuFeedbackMapper, GuFeedback> implements GuFeedbackService {

    @Autowired
    private GuFeedbackMapper feedbackMapper;

    @Autowired
    private GuUserService userService;
    @Autowired
    private GuFeedbackTypeService feedbackTypeService;

    @Override
    public PageVo<GuFeedback> backPage(int p, int size, int status, String searchKey) {
        IPage<GuFeedback> iPage = new Page<>(p, size);
        iPage = this.page(iPage, new LambdaQueryWrapper<GuFeedback>() {
            {
                eq(!ObjectUtils.isEmpty(searchKey), GuFeedback::getFeedbackSteamId, searchKey);
                eq(status != -1, GuFeedback::getFeedbackStatus, status);
            }
        });
//        iPage.getRecords().forEach(guFeedback -> {
//            guFeedback.setIteamName(feedbackTypeService.getById(guFeedback.getFeedbackId()).getTypeTitle());
//        });

        return new PageVo<>(iPage.getRecords(), iPage.getTotal());
    }

    @Override
    public boolean addition(GuFeedback feedback) {
        CommonUtil.checkParams(feedback.getFeedbackType(), feedback.getFeedbackContact(), feedback.getFeedbackDesc());
        GuUser user = userService.getById(feedback.getFeedbackUserId());
        feedback.setFeedbackSteamId(user.getUserSteamId());
        return feedback.insert();
    }
}
